package com.ykdl.member.kid.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.marketcard.PersonalDataActivity;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;

public class CutPicActivity extends Activity implements OnClickListener {

	private CutPicView mCutPicView;

	public static Bitmap bitmap;
	private String Path = "";
	public static String filePath="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cut_pic);
		Path = this.getIntent().getStringExtra("Path");
		initView();
	}

	private void initView() {
		mCutPicView = (CutPicView) findViewById(R.id.cut_pic_view);
		// BitmapDrawable bd = (BitmapDrawable)
		// getResources().getDrawable(R.drawable.demo);
		// Bitmap bitmap = bd.getBitmap();
		Bitmap bitmap = BitmapFactory.decodeFile(Path);
		mCutPicView.setBitmap(bitmap);
		
		findViewById(R.id.btn_done).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_done:
			bitmap = mCutPicView.toRoundBitmap();
			filePath=getPath();
			Intent intent = new Intent(CutPicActivity.this,
					PersonalDataActivity.class);
			setResult(101, intent);
			finish();
			break;

		default:
			break;
		}
	}

	public String getPath() {

		String dirFile = Environment.getExternalStorageDirectory().toString()
				+ "/hsh/bmpcache/";
		File dirfile = new File(dirFile);
		if (!dirfile.exists()) {
			dirfile.mkdirs();
		}
		File originalFile = new File(dirFile, System.currentTimeMillis()
				+ ".jpg");
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(originalFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		bitmap.compress(CompressFormat.JPEG, 80, fileOutputStream);
		return originalFile.getAbsolutePath();
	}

}
